package de.baumann.browser.fragments

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.activitys.order.WechartPayActivity
import de.baumann.browser.adapter.HashOrderAdapter
import de.baumann.browser.api.net.vo.Order
import de.baumann.browser.api.net.vo.PayParam
import de.baumann.browser.views.dialog.PayPasswordDialog

/**
 *@author:
 *@date:2018/12/19 14:39
 *@discription: 算力包 未支付订单
 */
class HashOrderUnpaidFragment : BaseHashOrderFragment(), PayPasswordDialog.InputPayPasswordCallBack {
    override val pageNum: Int
        get() = 0

    lateinit var payPasswordDialog: PayPasswordDialog
    lateinit var tradeNo: String

    companion object {
        val instance: HashOrderUnpaidFragment by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            HashOrderUnpaidFragment()
        }
    }

    override fun initView(view: View) {
        super.initView(view)

        hashOrderAdapter.setOnOrderClickListener(object : HashOrderAdapter.OnOrderClickListener {
            override fun onCancelOrder(tradeNo: String?) {
                getPresenter().cancelOrder(tradeNo!!)
            }

            override fun onRepaid(tradeNo: String?, patMode: Int) {
                this@HashOrderUnpaidFragment.tradeNo = tradeNo!!
                if (patMode == 7) {
                    payPasswordDialog = PayPasswordDialog(mContext)
                    payPasswordDialog.inputPayPasswordCallBack = this@HashOrderUnpaidFragment
                    payPasswordDialog.show()
                } else {
                    getPresenter().odinPay("")
                }
            }
        })
    }

    override fun getAdapter(): HashOrderAdapter {
        return HashOrderAdapter(R.layout.recycler_item_hash_order_unpain, 0)
    }


    override fun initData() {
        getPresenter().getUnpaidHashOrder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1411 && resultCode == RESULT_OK) {
            initData()
        }
    }

    override fun setOrders(orders: List<Order>?) {
        hashOrderAdapter.setNewData(orders)
    }

    override fun orderCancelled() {
        initData()
    }

    override fun inputedPassword(password: String) {
        getPresenter().odinPay(password)
    }

    override fun getPayId(): String {
        return tradeNo
    }

    override fun paySuccess() {
        payPasswordDialog.dismiss()
        initData()
    }

    override fun wxPay(payParam: PayParam?) {
        if (payParam == null) {
            return
        }
        val intent = Intent(context, WechartPayActivity::class.java)
                .apply {
                    putExtra("payParam", payParam)
                }
        startActivityForResult(intent, 1411)
    }
}